﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_UserInfoLeft : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Message.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 29)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            SectionServices sectionServices = new SectionServices();
            List<Section> list = sectionServices.findAllSection();
            List<Section> baselist = new List<Section>();
            foreach (Section s in list)
            {
                if (s.BaseSection.SectionId == 0)
                {
                    baselist.Add(s);
                }
            }
            foreach (Section s in baselist)
            {
                TreeNode tn = new TreeNode(s.SectionName);
                tn.Value = s.SectionId.ToString();
                tn.ImageUrl = @"~/images/Sections.png";
                tn.SelectAction = TreeNodeSelectAction.Expand;
                TreeView1.Nodes.Add(tn);
                AddUserNode(s.SectionId, tn);
                AddTreeSonNode(s.SectionId, tn);
            }
        }
    }
    void AddTreeSonNode(int baseid, TreeNode treeNode)
    {
        List<Section> list = SectionServices.findByBaseSection(baseid);
        if (list.Count > 0)
        {
            foreach (Section s in list)
            {
                TreeNode tn = new TreeNode(s.SectionName);
                tn.Value = s.SectionId.ToString();
                tn.ImageUrl = @"~/images/Sections.png";
                tn.SelectAction = TreeNodeSelectAction.Expand;
                treeNode.ChildNodes.Add(tn);
                AddUserNode(s.SectionId, tn);
                AddTreeSonNode(s.SectionId, tn);
            }
        }
    }
    void AddUserNode(int baseid, TreeNode treeNode)
    {
        UserManager um = new UserManager();
        List<TarreOA.TarreOAModels.User> list = um.findBySectionId(baseid);
        if (list.Count>0)
        {
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                if (u.State)
                {
                    TreeNode tn = new TreeNode(u.UserName);
                    tn.Value = u.LoginName;
                    tn.ImageUrl = @"~/images/Sections.png";
                    tn.Target = "userright";
                    tn.NavigateUrl = "UserInfo.aspx?loginName=" + u.LoginName+"&back=1";
                    treeNode.ChildNodes.Add(tn);
                }
            }
        }
    }
}
